<?php

/**
 * IndexController is the default controller for this application
 */
class BannerController extends ControllerBase {
	/**
	 * BannerDataService object
	 * 
	 * @var BannerDataService object
	 */
	public $bannerDataService = null;
	
	/**
	 * Constructor of This Controller
	 */
	public function __construct() {
		parent::__construct ();
		
		// Set page name
		$this->setPageName ( 'settings' );
		
		$this->bannerDataService = new BannerDataService ();
		
		$this->smarty->assign ( 'statusList', BannerEntity::getStatusList () );
	}
	
	/**
	 * The "index" action is the default action for all controllers -- the
	 * landing page of the site.
	 *
	 * @return void
	 */
	public function indexAction() {
		// Get all banners which will be shown on homepage
		$arrElement = array ();
		$arrElement ['ownerId'] = 0;
		
		$this->smarty->assign ( 'itemList', $this->bannerDataService->getItems ( $arrElement, 'createdDate DESC' ) );
		
		// Create new token key
		$this->createActionKey ();
		
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/Banner/index.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
	
	public function editAction() {
		// Add code here
	}
	
	public function addAction() {
		// Get action method and check it
		$actionForm = $this->getPost ( 'actionForm', '' );
		if (! empty ( $actionForm ) && $actionForm == 'submit') {
			$title = $this->getPost ( 'title', '' );
			$description = $this->getPost ( 'description', '' );
			$url = $this->getPost ( 'url', '' );
			$status = $this->getPost ( 'status', '' );
			
			// Upload picture
			$picture = '';
			try {
				$picture = CommonUtils::uploadBanner ( 'picture' );
			} catch ( Exception $ex ) {
				
				// Ignore this case
				$resultMessage = 'Upload banner fail.';
			}
			
			if (! empty ( $picture )) {
				try {
					$bannerEntity = $this->bannerDataService->beforeCreate ();
					$bannerEntity->title = $title;
					$bannerEntity->picture = $picture;
					$bannerEntity->description = $description;
					$bannerEntity->url = $url;
					$bannerEntity->status = $status;
					// This will add banner for Main Page
					$bannerEntity->ownerId = 0;
					$bannerEntity->viewed = 0;
					$this->bannerDataService->create ( $bannerEntity );
					$resultMessage = 'The record has been saved successfully';
				} catch ( Exception $ex ) {
					$resultMessage = 'The record cannot create';
				}
			}
			
			$this->smarty->assign ( 'resultMessage', $resultMessage );
		}
		
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/Banner/add.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
	
	/**
	 * Set status to inactive
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function inactiveAction() {
		$this->updateStatus ( BannerEntity::INACTIVE );
		
		// View inactive list
		$this->indexAction ();
	}
	
	/**
	 * Set status to active
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function activeAction() {
		$this->updateStatus ( BannerEntity::ACTIVE );
		
		// View banner list
		$this->indexAction ();
	}
	
	/**
	 * Delete action is trigger when administrator want to delete a banner
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function deleteAction() {
		if ($this->validateActionKey ()) {
			
			// Get Id of banner
			$id = $this->getPost ( 'idValue', '' );
			
			// Delete a banner item
			$res = $this->bannerDataService->del ( $id );
		}
		
		// View banner list
		$this->indexAction ();
	}
	
	/**
	 * Delete list of banner
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function deleteallAction() {
		if ($this->validateActionKey ()) {
			$iCount = $this->getPost ( 'iCount', 0 );
			for($i = 0; $i < $iCount; $i ++) {
				$id = $this->getPost ( 'checkItem' . $i, '' );
				if (! empty ( $id )) {
					
					// Delete a banner item
					$this->bannerDataService->del ( $id );
				}
			}
		}
		
		// View banner list
		$this->indexAction ();
	}
	
	/**
	 * Update status list of banner
	 *
	 * @access private
	 *
	 * @param int $status: Status of the banner
	 *
	 * @return void
	 */
	protected function updateStatus($status) {
		if ($this->validateActionKey ()) {
			$iCount = $this->getPost ( 'iCount', 0 );
			for($i = 0; $i < $iCount; $i ++) {
				$id = $this->getPost ( 'checkItem' . $i, '' );
				if (! empty ( $id )) {
					$this->bannerDataService->updateStatus ( $id, $status );
				}
			}
		}
	}
}
